package net.huashitong.user.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.user.entity.Role;
import net.huashitong.user.filter.RoleFilter;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleDao extends StringDao<Role> {

    public List<Role> queryByFilter(RoleFilter filter){
        GenericQuery query = createQuery("obj");
        query = queryParam(query,filter);
        return query.listResult(filter);
    }

    public List<Role> queryByFilterNoPage(RoleFilter filter){
        GenericQuery query = createQuery("obj");
        query = queryParam(query,filter);
        return query.listResult();
    }

    //查询条件
    public GenericQuery queryParam(GenericQuery query, RoleFilter filter){
        if (ObjectUtils.isNotBlank(filter.getRoleName())) {
            query.append(" and obj.roleName like:roleName").setParam("roleName",
                    StringUtils.getLikewords(filter.getRoleName()));
        }
        if (ObjectUtils.isNotBlank(filter.getRoleAuth())) {
            query.append(" and obj.roleAuth =:roleAuth").setParam("roleAuth",
                    filter.getRoleAuth());
        }
        query.append(" order by obj.sysId asc");
        return query;
    }

    public List<Role> queryRoleExist(String roleName){
        GenericQuery query = createQuery("obj");
        query.append(" and obj.roleName =:roleName").setParam("roleName", roleName);
        return query.listResult();
    }
}
